package utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;

import java.util.LinkedHashMap;
import java.util.prefs.Preferences;

/**
 * Created by thormentor on 19-05-2014.
 */
public class Utils {
    public static final GsonBuilder GSON_BUILDER = new GsonBuilder()
            .setPrettyPrinting();

    private static Gson gson;

    public static Gson getGson() {
        if (gson == null)
            gson = GSON_BUILDER.create();

        return gson;
    }

    public static <K, V> ObservableMap<K, V> observableMap() {
        return FXCollections.observableMap(new LinkedHashMap<K, V>());
    }

    public static void output(Object o) {
        System.out.println(getGson().toJson(o));
    }

    public static <T> T preferred(Class<?> aClass, String key, String def, Class<T> classOfT) {
        String json = Preferences.userNodeForPackage(aClass).get(key, def);
        return getGson().fromJson(json, classOfT);
    }
}
